﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MessageWorkflow.Handler
{
    public class TemporaryWorkflowHandler : IWorkflowHandler
    {
        protected IWorkflowHandler InnerHandler { get; private set; }

        protected WorkflowChannel Channel { get; private set; }

        public TemporaryWorkflowHandler(WorkflowChannel channel, IWorkflowHandler handler)
        {
            if (channel == null)
            {
                throw new ArgumentNullException("channel");
            }
            if (handler == null)
            {
                throw new ArgumentNullException("handler");
            }

            Channel = channel;
            InnerHandler = handler;
        }

        public bool CanHandle(Type type)
        {
            return InnerHandler.CanHandle(type);
        }

        public IAsyncResult BeginHandle(AsyncCallback callback, object message)
        {
            return InnerHandler.BeginHandle(callback, message);
        }

        public void EndHandle(IAsyncResult result)
        {
            InnerHandler.EndHandle(result);
            Channel.UnsubscribeHandler(this);
        }
    }
}
